import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  getToken,
  setToken as _setToken,
  removeToken
} from '@/utils/cache/cookies'
import { useSettingsStore } from './settings'
import { useTagsViewStore } from './tags-view'
import { resetRouter } from '@/router'
import { routerConfig } from '@/router/config'
import { getCurrentUserApi } from '@/services/user'

export const useUserStore = defineStore('user', () => {
  const token = ref(getToken() || '')
  const username = ref<string>('')
  const userId = ref<number>()
  const roles = ref<string[]>([])
  const avatar = ref<string>('')

  const tagsViewStore = useTagsViewStore()
  const settingsStore = useSettingsStore()

  // 设置 Token
  const setToken = (value: string) => {
    _setToken(value)
    token.value = value
  }

  // 获取用户详情
  const getInfo = async () => {
    const { data } = await getCurrentUserApi(userId.value)
    username.value = data.username
    avatar.value = data.avatar
    // 验证返回的 roles 是否为一个非空数组，否则塞入一个没有任何作用的默认角色，防止路由守卫逻辑进入无限循环
    roles.value =
      data.roles?.length > 0 ? data.roles : routerConfig.defaultRoles
  }

  // 模拟角色变化
  const changeRoles = (role: string) => {
    const newToken = `token-${role}`
    token.value = newToken
    _setToken(newToken)
    // 用刷新页面代替重新登录
    location.reload()
  }

  // 登出
  const logout = () => {
    removeToken()
    token.value = ''
    roles.value = []
    resetRouter()
    resetTagsView()
  }

  // 重置 Token
  const resetToken = () => {
    removeToken()
    token.value = ''
    roles.value = []
  }

  // 重置 Visited Views 和 Cached Views
  const resetTagsView = () => {
    if (!settingsStore.cacheTagsView) {
      tagsViewStore.delAllVisitedViews()
      tagsViewStore.delAllCachedViews()
    }
  }

  return {
    token,
    roles,
    username,
    avatar,
    userId,
    setToken,
    getInfo,
    changeRoles,
    logout,
    resetToken
  }
})
